<template>
    <el-card class="w-full pt-[5px]">
        <el-radio-group v-model="planType" @change="getList(1)">
            <el-radio-button v-for="item in statisticsList" :key="item.value" :value="item.value">{{ item.name }}({{
            item.count
        }})</el-radio-button>
        </el-radio-group>
        <el-form :model="searchForm" class="mt-[20px]" label-width="110px">
            <el-row :gutter="20">
                <el-col :span="4">
                    <el-form-item label="论文名称" class="mb-[0px]">
                        <el-input v-model="searchForm.name" clearable />
                    </el-form-item>
                </el-col>
                <el-col :span="4">
                    <el-form-item label="收录类型" class="mb-[0px]">
                        <el-select v-model="searchForm.embodyTypeId" clearable>
                            <el-option v-for="item in slList" :key="item.code" :label="item.name" :value="item.code" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="10">
                    <el-form-item label="收录时间" class="mb-[0px]">
                        <!-- <el-date-picker v-model="valueDate1" format="YYYY/MM/DD" value-format="YYYY-MM-DD"
                            type="daterange" range-separator="至" /> -->
                        <CommonDateSelect placeholder="收录时间" v-model:beginTime="searchForm.embodyDateStart"
                            v-model:endTime="searchForm.embodyDateEnd" v-model:dateformat="searchForm.dateFormat" />
                    </el-form-item>
                </el-col>
                <el-col :span="4">
                    <el-form-item label="审核状态">
                        <el-select v-model="searchForm.checkstatus" clearable>
                            <el-option v-for="item in shztList" :key="item.code" :label="item.name"
                                :value="item.code" />
                        </el-select>
                        <!-- <CustomTreeSelect placeholder="审核状态" v-model="searchForm.checkstatus" valueKey="code"
                            labelKey="name" :isFetch="false" :dataList="shztList" /> -->
                    </el-form-item>
                </el-col>

                <el-col :span="2">
                    <div class="flex justify-end">
                        <el-button type="primary" @click="queryMet">查询</el-button>
                    </div>
                </el-col>
            </el-row>
        </el-form>
    </el-card>
    <el-card class="w-full pt-[5px] mt-[10px]">
        <div class="mb-[20px]">
            <el-button @click="handleDel">
                <el-icon>
                    <DeleteFilled />
                </el-icon>
                删除
            </el-button>
        </div>
        <el-table :data="tableData" class="w-full" @selection-change="handleSelectionChange" stripe>
            <el-table-column type="selection" width="55" :selectable="checkSelectable" />
            <el-table-column prop="paperName" label="论文名称">
                <template #default="scope">
                    <el-link type="primary" @click="viewItem(scope.row)">{{ scope.row.paperName }}</el-link>
                </template>
            </el-table-column>
            <el-table-column prop="embodyTypeId" label="收录类型" :formatter="formatbznbId" />
            <el-table-column prop="embodyDate" label="收录日期" :formatter="dateFormatter2" />
            <el-table-column prop="checkstatus" label="审核状态">
                <template #default="scope">
                    <Feedback :id="scope.row.checkstatus" :list="shztList">
                    </Feedback>
                </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="300" align="center">
                <template #default="scope">
                    <el-button type="primary" size="small" v-equalRole="['biz_kyry']"
                        v-show="scope.row.checkstatus == '-1' || scope.row.checkstatus == '3'" plain
                        @click="handleBackTrack(scope.row)">
                        编辑
                    </el-button>
                    <el-button type="success" v-show="scope.row.isAuditor" v-equalRole="['biz_kycgly', 'biz_kyms']"
                        size="small" @click="handleCheck(scope.row.id, scope.row.processInstanceId)" plain>
                        审核
                    </el-button>
                    <el-button type="warning" size="small" v-equalRole="['biz_kyry']" plain
                        @click="handleCancel(scope.row.processInstanceId)" :loading="processLoading"
                        v-show="scope.row.checkstatus == 1">撤销</el-button>
                    <el-button type="danger" size="small"
                        v-show="scope.row.checkstatus == '-1' || scope.row.checkstatus == '3'" plain
                        @click="delHonor(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="mt-[20px] flex justify-end">
            <pagination v-show="total > 0" :total="total" v-model:page="pageInfo.pageNum"
                v-model:limit="pageInfo.pageSize" @pagination="getList" />
        </div>
    </el-card>
    <viewModal v-model="ViewdialogVisible" :slList="slList" ref="viewModalRef"></viewModal>
    <backTrackModal v-model="viewSbModalVisible" :id="backTrackId"></backTrackModal>
    <CheckModal v-model="CheckModalVisible" ref="checkModalRef" :slList="slList" @callBack="callBackFun" />
</template>

<script setup>
import { onMounted } from 'vue';
import { getCategory } from "@/api/system/category"
import { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import { getpaperEmbodyList, paperEmbodyDel, getpaperEmbodyInfo, getlevelList, getpaperEmbodyListStatistics } from "@/api/achievement/kycg"
import viewModal from "./viewModal.vue";
import CommonDateSelect from "@/views/achievement/components/CommonDateSelect";
import Cookies from 'js-cookie'
import CheckModal from "./CheckModal.vue";
import { cancelProcess } from "@/api/project/common";
import backTrackModal from "../xslw/backTrackModal.vue";
import CustomTreeSelect from "@/views/project/components/CustomTreeSelect.vue";
import useCategoryStore from "@/store/modules/category";

const categoryStore = useCategoryStore();
const { shztList } = storeToRefs(categoryStore); // 解构状态
const { getshztList } = categoryStore; // 解构方法

const router = useRouter();
const planType = ref("");
const editModelVisible = ref(false);
const ViewdialogVisible = ref(false);
const CheckModalVisible = ref(false);

const viewModalRef = ref(null)
const viewItem = (i) => {
    getpaperEmbodyInfo(i.id).then((res) => {
        if (res.code == 200) {
            ViewdialogVisible.value = true;
            viewModalRef.value.getRuleForm(res.data)
        }
    })
};
const searchForm = ref({
    name: '',
    embodyTypeId: '',
    checkstatus: '',
    embodyDateStart: "",
    embodyDateEnd: "",
    dateFormat: 'yyyy-MM-dd',
});
const pageInfo = ref({
    pageNum: 1,
    pageSize: 10
})

const total = ref(0)
const tableData = ref([]);
const EditModalRef = ref(null)
const multipleSelection = ref([])
const handleSelectionChange = (val) => {
    multipleSelection.value = val
}
//点击删除
const handleDel = () => {
    if (multipleSelection.value.length == 0) {
        ElMessage.warning("请选择要删除的数据")
        return;

    } else {
        ElMessageBox.confirm("是否确认删除？", "提示", {
            type: "warning",
        }).then(() => {
            let ids = multipleSelection.value.map(item => item.id)
            ids = ids.join(',')
            paperEmbodyDel(ids).then(res => {
                if (res.code == 200) {
                    getList()
                    ElMessage.success('删除成功')
                }
            })
        });
    }

};

const formatbznbId = (row, column, cellValue, index) => {
    if (slList.value.length && cellValue) {
        if (slList.value.find(item => item.code == cellValue)) {
            return slList.value.find(item => item.code == cellValue).name
        } else {
            return ''
        }
    } else {
        return ''
    }
}
const initList = () => {
    getList()
}
const valueDate1 = ref('')


const reset = () => {
    valueDate1.value = ''
    searchForm.value = {
        name: '',
        checkstatus: '',
        embodyTypeId: '',
        embodyDateStart: "",
        embodyDateEnd: "",
        embodyDateformat: 'yyyy-MM-dd',
    }
}
const getList = async (i) => {

    getListStatisticsMet()
    if (i == 1) {
        pageInfo.value.pageNum = 1
    }
    console.log(searchForm.value)
    let parms = {
        ...searchForm.value,
        ...pageInfo.value,
        leftCheckstatus: planType.value,
    }
    // parms.checkstatus = searchForm.value.checkstatus.join(',')
    let res = await getpaperEmbodyList(parms)
    if (res.code == 200) {
        tableData.value = res.rows
        total.value = res.total

    }

}
const slList = ref([])
const getslList = async () => {
    let parms = {
        pageNum: 1,
        pageSize: 100
    }
    let res = await getlevelList(parms)
    if (res.code == 200) {
        slList.value = res.rows.filter(item => {
            return item.isEmbody == '1'
        })
    }
}
const delHonor = (i) => {
    ElMessageBox.confirm(
        '确认！删除这条数据?',
        '提醒',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            paperEmbodyDel(i.id).then(res => {
                if (res.code == 200) {
                    getList()
                    ElMessage.success('删除成功')
                }
            })


        })
        .catch(() => {

        })

}

const checkModalRef = ref();
//点击审核
const handleCheck = (id, processId) => {
    getpaperEmbodyInfo(id).then((res) => {
        if (res.code == 200) {
            CheckModalVisible.value = true;
            checkModalRef.value.getRuleForm(res.data, processId);
        }
    });
};

const processLoading = ref(false);
//点击撤销
const handleCancel = async (processId) => {
    try {
        processLoading.value = true;
        const res = await cancelProcess(processId);
        if (res.data) {
            ElMessage({
                message: "操作成功！",
                type: "success",
            });
            initList();
        } else {
            ElMessage({
                message: "操作失败！",
                type: "warning",
            });
        }
        processLoading.value = false;
    } catch (error) {
        console.log(error);
    }
};

const callBackFun = () => {
    getList();
};

const viewSbModalVisible = ref(false);
const backTrackId = ref("");
const handleBackTrack = (i) => {
    backTrackId.value = i.id;
    viewSbModalVisible.value = true;
};

const statisticsList = ref([{
    name: "全部",
    value: '',
    count: 0
},
{
    name: "审批通过",
    value: '2',
    count: 0
},
{
    name: "审批不通过",
    value: '3',
    count: 0
},
{
    name: "审批中",
    value: '1',
    count: 0
}])

const getListStatisticsMet = async () => {
    let parms = {
        ...searchForm.value,
    };
    let res = await getpaperEmbodyListStatistics(parms);
    if (res.code == 200) {
        statisticsList.value.forEach(item => {
            if (item.name != '全部') {
                let hasStatis = res.data.filter(item1 => {
                    return item1.checkSatus == item.value
                })
                hasStatis && hasStatis.length ? item.count = hasStatis[0].count : item.count = 0
            }


        })
        let allNum = 0
        res.data.forEach(item => {
            allNum += item.count
        })
        statisticsList.value[0].count = allNum
    }
}
const queryMet = () => {
    getList(1)
}
const checkSelectable = (row, index) => {
    return row.checkstatus != '1';
}


onMounted(() => {
    if (!shztList.value.length) {
        getshztList()
    }
    initList()
    getslList()
})

</script>